﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace PeisongWeb.admin
{
    public partial class grossprofit : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("ED", 1))
                {
                    rptProductBind();
                    rptCategoryBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        #region  按类别分析
        void rptCategoryBind()
        {
            StringBuilder where2 = new StringBuilder();
            where2.Append("[status]='4'");
            if (ext_StartInputDt2.SelectedDate != DateTime.MinValue)
                where2.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')<=0 ", ext_StartInputDt2.SelectedDate);
            if (ext_EndInputDt2.SelectedDate != DateTime.MinValue)
                where2.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')>=0 ", ext_EndInputDt2.SelectedDate);
            if (txtBuyer.Text.Trim() != "")
                where2.AppendFormat(" and company like '%{0}%'", txtBuyer.Text.Trim());
            if (txtSeller.Text.Trim() != "")
                where2.AppendFormat(" and sellerName like '%{0}%'", txtSeller.Text.Trim());
            DataSet ds2 = logic.orderItem.listwithCategoryGrossprofit(where2.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds2.Tables[0].Rows[0][0]);
            rptCategory_list.DataSource = ds2.Tables[1];
            rptCategory_list.DataBind();

            DataTable dtTotal = ds2.Tables[2];
            Label1.Text = string.Format("{0:F2}", dtTotal.Rows[0]["sumSellerPrice"]);
            Label2.Text = string.Format("{0:F2}", dtTotal.Rows[0]["sumPrice"]);
        }
        protected void lbtn_Search2_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptCategoryBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptCategoryBind();
        }

        protected void lbn_ImportByC_Click(object sender, EventArgs e)
        {
            StringBuilder where2 = new StringBuilder();
            where2.Append("[status]='4'");
            if (ext_StartInputDt2.SelectedDate != DateTime.MinValue)
                where2.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')<=0 ", ext_StartInputDt2.SelectedDate);
            if (ext_EndInputDt2.SelectedDate != DateTime.MinValue)
                where2.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')>=0 ", ext_EndInputDt2.SelectedDate);
            if (txtBuyer.Text.Trim() != "")
                where2.AppendFormat(" and company like '%{0}%'", txtBuyer.Text.Trim());
            if (txtSeller.Text.Trim() != "")
                where2.AppendFormat(" and sellerName like '%{0}%'", txtSeller.Text.Trim());
            DataSet ds2 = logic.orderItem.listwithCategoryGrossprofit(where2.ToString(), 0, 1);
            //调用xlsToExcel方法生成Excel报表
        }

        #endregion

        #region  按商品分析
        //private decimal sellerPrice = 0m;
        //private decimal price = 0m;
        void rptProductBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("[status]='4'");
            if (ext_StartInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')<=0 ",ext_StartInputDt.SelectedDate);
            if (ext_EndInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')>=0 ", ext_EndInputDt.SelectedDate);
            if (txt_productName.Text.Trim() != "")
                where.AppendFormat(" and proName like '%{0}%'", txt_productName.Text.Trim());
            if (txt_buyer.Text.Trim() != "")
                where.AppendFormat(" and company like '%{0}%'", txt_buyer.Text.Trim());
            if (txt_seller.Text.Trim() != "")
                where.AppendFormat(" and sellerName like '%{0}%'", txt_seller.Text.Trim());
            DataSet ds = logic.orderItem.listwithProductGrossprofit(where.ToString(), AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            AspNetPager2.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
            DataTable dtTotal = ds.Tables[2];
            lbl_sellerPriceTotal.Text = string.Format("{0:F2}", dtTotal.Rows[0]["sumSellerPrice"]);
            lbl_priceTotal.Text = string.Format("{0:F2}", dtTotal.Rows[0]["sumPrice"]);
        }
       
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            AspNetPager2.CurrentPageIndex = 1;
            rptProductBind();
        }
        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            rptProductBind();
        }

        protected void lbn_Import_Click(object sender, EventArgs e)
        {
            StringBuilder where = new StringBuilder();
            where.Append("[status]='4'");
            if (ext_StartInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')<=0 ", ext_StartInputDt.SelectedDate);
            if (ext_EndInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" AND DATEDIFF(DAY,inputdt,'{0}')>=0 ", ext_EndInputDt.SelectedDate);
            if (txt_productName.Text.Trim() != "")
                where.AppendFormat(" and proName like '%{0}%'", txt_productName.Text.Trim());
            if (txt_buyer.Text.Trim() != "")
                where.AppendFormat(" and company like '%{0}%'", txt_buyer.Text.Trim());
            if (txt_seller.Text.Trim() != "")
                where.AppendFormat(" and sellerName like '%{0}%'", txt_seller.Text.Trim());
            DataSet ds = logic.orderItem.listwithProductGrossprofit(where.ToString(),0,1);
            //调用xlsToExcel方法生成Excel报表
        }

        #endregion

     
    }
}
